package com.xzx.framework.util.email;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;  
import javax.mail.Transport;  
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import com.sun.mail.util.MailSSLSocketFactory;
/**
 * 简易的邮件发送工具类
* @ClassName: Email    
* @Description:     
* @author xzx   
* @date 2017年12月29日 下午4:01:27    
*
 */
public class SendEmailUtil {  
	/**
	 * 发送邮件的服务器地址
	 */
	private final static String host = "smtp.exmail.qq.com" ; 
	/**
	 * 发送邮件的服务器端口
	 */
	private final static int port = 465;
	/**
	 * 发送邮件的邮箱号
	 */
	private final static String account = "yaolai-notice@yaolaivip.com";
	/**
	 * 发送邮件的邮箱密码
	 */
	private final static String pass = "123yaolaiVIP";
	/**
	 * 协议
	 */
    private final static String protocol = "smtp"; 
	
	    static class MyAuthenricator extends Authenticator{  
	        String u = null;  
	        String p = null;  
	        public MyAuthenricator(String u,String p){  
	            this.u=u;  
	            this.p=p;  
	        }  
	        @Override  
	        protected PasswordAuthentication getPasswordAuthentication() {  
	            return new PasswordAuthentication(u,p);  
	        }  
	    }
	    
	    private String to;    //收件人
	    private String subject;    //主题
	    private String content;    //内容
	    private String fileStr;    //附件路径

	    public SendEmailUtil(String to, String subject, String content, String fileStr) {
	        this.to = to;
	        this.subject = subject;
	        this.content = content;
	        this.fileStr = fileStr;
	    }

	    public void send(){
	        Properties prop = new Properties();
	        //协议
	        prop.setProperty("mail.transport.protocol", protocol);
	        //服务器
	        prop.setProperty("mail.smtp.host", host);
	        //端口
	        prop.setProperty("mail.smtp.port", port+"");
	        //使用smtp身份验证
	        prop.setProperty("mail.smtp.auth", "true");
	        //使用SSL，企业邮箱必需！
	        //开启安全协议
	        MailSSLSocketFactory sf = null;
	        try {
	            sf = new MailSSLSocketFactory();
	            sf.setTrustAllHosts(true);
	        } catch (GeneralSecurityException e1) {
	            e1.printStackTrace();
	        }
	        prop.put("mail.smtp.ssl.enable", "true");
	        prop.put("mail.smtp.ssl.socketFactory", sf);
	        
	        Session session = Session.getDefaultInstance(prop, new MyAuthenricator(account, pass));
	        session.setDebug(true);
	        MimeMessage mimeMessage = new MimeMessage(session);
	        try {
	            //发件人
//	            mimeMessage.setFrom(new InternetAddress(account,"XXX"));        //可以设置发件人的别名
	            mimeMessage.setFrom(new InternetAddress(account));    //如果不需要就省略
	            //收件人
	            mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
	            //主题
	            mimeMessage.setSubject(subject);
	            //时间
	            mimeMessage.setSentDate(new Date());
	            //容器类，可以包含多个MimeBodyPart对象
	            Multipart mp = new MimeMultipart();
	            
	            //MimeBodyPart可以包装文本，图片，附件
	            MimeBodyPart body = new MimeBodyPart();
	            //HTML正文
	            body.setContent(content, "text/html; charset=UTF-8");
	            mp.addBodyPart(body);
	            
	            //添加图片&附件
	            body = new MimeBodyPart();
//	            body.attachFile(fileStr);
	            mp.addBodyPart(body);
	            
	            //设置邮件内容
	            mimeMessage.setContent(mp);
	            //仅仅发送文本
	            mimeMessage.setText(content);
//	            mimeMessage.saveChanges();
	            Transport.send(mimeMessage);
	        } catch (MessagingException e) {
	            e.printStackTrace();
	        } 
//	        catch (UnsupportedEncodingException e) {
//	            e.printStackTrace();
//	        } catch (IOException e) {
//	            e.printStackTrace();
//	        }
	    }
    
    public static void main(String[] args) {
    	SendEmailUtil e = new SendEmailUtil("496091383@qq.com", "测试邮件", "ceshyoujian1", null);
    	e.send();
	}
} 
